angular.module('app')
	.service('AppDictService',['Dictionary', function(Dictionary){
		var app = this;
		this.config = function(scope, tAttrs){
			scope.multiple = angular.isDefined(tAttrs.multiple) ? (tAttrs.multiple === 'false' ? false : true ) : false;
			scope.multiple = angular.isDefined(tAttrs.appDictCheckbox) ? true : scope.multiple;			
			scope.array = angular.isDefined(tAttrs.array) ? (tAttrs.array === 'false' ? false : true ) : false;
			scope.separator = (scope.separator) ? scope.separator : ',';
		}
		this.getDicts = function(dictType){
			if(!Dictionary || !Dictionary.DictionaryItems) return [];
			return Dictionary.DictionaryItems[dictType];
		}
		this.init = function(scope, tAttrs) {
			scope.dicts = app.getDicts(scope.dictType);
			app.config(scope, tAttrs);
			if(!scope.model && scope.model !== false && scope.defaultValue) scope.model = scope.defaultValue;
		}
		this.setChecBox = function(scope, value){
			var val = angular.isString(value) ? value.split(scope.separator) : value;
			for(var index in val) {
				for(var jindex in scope.dicts) {
					if(scope.dicts[jindex]['value'] === val[index]) {
						scope.dicts[jindex]['checked'] = true;
					}
				}
			}
		}
		this.initChecBox = function(scope, tAttrs){
			scope.dicts = [];
			angular.copy(app.getDicts(scope.dictType), scope.dicts);
			app.config(scope, tAttrs);
			if(scope.defaultValue) {
				app.setChecBox(scope, scope.defaultValue);
			}
			var unModelWatch = scope.$watch('model', function(newVal){
				if(!newVal) return;
				app.setChecBox(scope, scope.model);
				unModelWatch();
			});
			scope.$watch('dicts', function(newVal){
				var val = [];
				for(var index in scope.dicts) {
					if(scope.dicts[index]['checked'] === true) {
						val.push(scope.dicts[index]['value']);
					}
				}
				scope.model = scope.array ? val : val.join(scope.separator);
			}, true);
			
		}
		this.viewDict = function(value, dictType,scope, tAttrs) {
			var list = app.getDicts(dictType);
			if(!list) return null;
			app.config(scope, tAttrs);
			var val = [];
			if(scope.multiple && scope.array) {
				val = value;
			} else if(scope.multiple) {
				val = value.split(scope.separator);
			} else {
				val = [value];
			}
			var res = [];
			for(var index in val) {
				var temp = val[index];
				for (var i = 0; i < list.length; i++) {
					var entity = list[i];
					if (entity.value == temp) {
						res.push(entity.label);
						break;
					}
				}
			}
	        return res.join(scope.separator + "  ");
		}
		
	}]);
angular.module('app')
.directive('appDict', ['$timeout', '$parse', 'uiLoad', 'JQ_CONFIG', function ($timeout, $parse, uiLoad, JQ_CONFIG) { // 单选
	return {
		restrict : "A",
		template : "{{ showText }}",
		controller : "AppDictController",
		scope : {
			model : '=appDict', 			// 绑定的外部数据model
			dictType : '@dictType'
		},
		link : function($scope, element, attrs, ctrl) { // 控制器执行后执行link
			$(element).addClass('app-dict-show');
			$scope.unWatch = angular.isDefined(attrs.unwatch) ? (attrs.unwatch === 'false' ? false : true ) : true;
			var unbingWatch = $scope.$watch('model', function(newVal, oldVal){
				if(!newVal && newVal !== false) return ;
				$scope.showText = ctrl.viewDict(newVal, $scope.dictType, $scope, attrs);
				 if($scope.unWatch) {
					 unbingWatch();
				 }
			});
		}
	}
}]);

angular.module('app')
	.controller('AppDictController', ['$scope', 'AppDictService', function($scope, AppDictService){
		this.init = AppDictService.init;
		this.initChecBox = AppDictService.initChecBox;
		this.viewDict = AppDictService.viewDict;
	}]);
angular.module('app')
    .directive('appDictRadio', ['$timeout', '$parse', function ($timeout, $parse) { // 单选
		return {
			restrict : "A",
			scope : {
				model : '=appDictRadio', 			// 绑定的外部数据model
				dictType : '@dictType',
				defaultValue: '=defaultValue',
				fieldName: '@fieldName', 			// 绑定到input的name
				disabled:'=disabled',
				ngRequired: '@required',			// 是否必填
				inline: '=inline'					// 是否在一行内显示,默认为true
			},
			templateUrl : 'tpl-common/directives/app-dict-radio.html',
			controller : "AppDictController",	
			link : function($scope, element, attrs, ctrl) { // 控制器执行后执行link
				$(element).addClass('app-dict-radio');
				if (!$scope.fieldName) {
					$scope.fieldName = 'dict_radio_' + Math.round(Math.random() * Math.pow(10, 10));
				}
				if (!$scope.inline) {
					$scope.inline = true;
				}
				
				ctrl.init($scope, attrs);
			}
		}
	}]);

angular.module('app')
.directive('appDictCheckbox', ['$timeout', '$parse', function ($timeout, $parse) { // 单选
	return {
		restrict : "A",
		scope : {
			model : '=appDictCheckbox', 			// 绑定的外部数据model
			dictType : '@dictType',
			defaultValue: '=defaultValue',
			fieldName: '@fieldName', 			// 绑定到input的name
			disabled:'=disabled',
			ngRequired: '@required',			// 是否必填
			array : "@array", 					// 值是否为数组
			separator : "@separator", 			// 多个值分隔符
			inline: '=inline'					// 是否在一行内显示,默认为true
		},
		templateUrl : 'tpl-common/directives/app-dict-checkbox.html',
		controller : "AppDictController",	
		link : function($scope, element, attrs, ctrl) { // 控制器执行后执行link
			$(element).addClass('app-dict-checkbox');
			if (!$scope.fieldName) {
				$scope.fieldName = 'checkbox' + Math.round(Math.random() * Math.pow(10, 10));
			}
			if (!$scope.inline) {
				$scope.inline = true;
			}
			
			ctrl.initChecBox($scope, attrs);
		}
	}
}]);

angular.module('app')
.directive('appDictSelect', ['$timeout', '$parse', function ($timeout, $parse) { // 单选
	return {
		restrict : "A",
		scope : {
			model : '=appDictSelect', 			// 绑定的外部数据model
			dictType : '@dictType',
			defaultValue: '=defaultValue',
			fieldName: '@fieldName', 			// 绑定到input的name
			showHeader: '@showHeader',			// 显示头部信息
			disabled:'=disabled',
			ngRequired: '@required'			// 是否必填
		},
		templateUrl : 'tpl-common/directives/app-dict-select.html',
		controller : "AppDictController",	
		link : function($scope, element, attrs, ctrl) { // 控制器执行后执行link
			$(element).addClass('app-dict-select');
			if (!$scope.fieldName) {
				$scope.fieldName = 'dict_select_' + Math.round(Math.random() * Math.pow(10, 10));
			}
			
			ctrl.init($scope, attrs);
		}
	}
}]);

